<script setup lang="ts">
import useUserStore from '@src/stores/useUserStore.ts'
import { storeToRefs } from 'pinia'
import { onMounted, ref, watch } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { TypeUser } from '@src/interfaces/TypeUser.ts'
import { updateUserApi } from '@src/apis/userApi.ts'
import TokenUtil from '@util/TokenUtil.ts'
import { EnumUserStatus } from '@src/enums/EnumUserStatus.ts'

const userStore = useUserStore()
let oldUserInfo = {} as TypeUser
const userInfo = ref<TypeUser>({ role: '普通用户', account: '', password: '' })
const inputVisible = ref({
  name: false,
  phone: false,
  password: false,
})
const inputRef = {
  name: ref<HTMLInputElement>(),
  phone: ref<HTMLInputElement>(),
  password: ref<HTMLInputElement>(),
}
onMounted(() => {
  TokenUtil.refresh().then(() => {
    userInfo.value = { ...storeToRefs(userStore).userInfo.value }
    oldUserInfo = { ...storeToRefs(userStore).userInfo.value, password: '' }
    watchUserInfo()
  })
})

const formRules = ref<FormRules<typeof userInfo>>({
  name: [{ max: 11, message: '不超过11位', trigger: 'blur' }],
  phone: [
    {
      pattern: /^1[3456789]\d{9}$/,
      message: '请输入正确的手机号',
      trigger: 'blur',
    },
  ],
  password: [{ max: 11, message: '不超过11位', trigger: 'blur' }],
})
const formRef = ref<FormInstance>()

const updateButtonVisible = ref(false)
const watchUserInfo = () => {
  watch(
    userInfo,
    () => {
      for (const key in userInfo.value) {
        // @ts-ignore
        if (userInfo.value[key] === oldUserInfo[key]) {
          continue
        }
        updateButtonVisible.value = true
        return
      }
      updateButtonVisible.value = false
    },
    {
      deep: true,
    },
  )
}
const updateUser = () => {
  formRef.value?.validate((valid) => {
    if (!valid) return
    updateUserApi(userInfo.value).then(({ data: newToken }) => {
      TokenUtil.setToken(newToken)
      oldUserInfo = { ...userInfo.value }
      updateButtonVisible.value = false
      ElMessage.success('更新成功')
    })
  })
}
</script>

<template>
  <div>
    <h2><el-text>个人信息</el-text></h2>
    <div class="form-box">
      <el-form
        ref="formRef"
        :model="userInfo"
        :rules="formRules"
        label-width="80"
        label-suffix=":"
      >
        <el-form-item label="账号" prop="account">
          <el-text>{{ userInfo.account }}</el-text>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-text
            v-if="userInfo.status === EnumUserStatus.ENABLE"
            type="success"
            >{{ userInfo.status }}</el-text
          >
          <el-text v-else type="danger">{{ userInfo.status }}</el-text>
        </el-form-item>
        <el-form-item label="用户名" prop="name">
          <el-input
            v-show="inputVisible.name"
            v-model="userInfo.name"
            ref="inputRef.name"
            @blur="inputVisible.name = false"
          ></el-input>
          <el-tooltip content="点击修改" placement="left">
            <el-text
              class="show-text"
              v-show="!inputVisible.name"
              @click="
                () => {
                  inputVisible.name = true
                  inputRef.name.value?.focus()
                }
              "
              >{{ userInfo.name }}</el-text
            >
          </el-tooltip>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-show="inputVisible.password"
            v-model="userInfo.password"
            ref="inputRef.password"
            @blur="inputVisible.password = false"
            show-password
          ></el-input>
          <el-tooltip content="点击修改" placement="left">
            <el-text
              class="show-text"
              v-show="!inputVisible.password"
              @click="
                () => {
                  inputVisible.password = true
                  inputRef.password.value?.focus()
                }
              "
              >******</el-text
            >
          </el-tooltip>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-show="inputVisible.phone"
            v-model="userInfo.phone"
            ref="inputRef.phone"
            @blur="inputVisible.phone = false"
          ></el-input>
          <el-tooltip content="点击修改" placement="left">
            <el-text
              class="show-text"
              v-show="!inputVisible.phone"
              @click="
                () => {
                  inputVisible.phone = true
                  inputRef.phone.value?.focus()
                }
              "
              >{{ userInfo.phone }}</el-text
            >
          </el-tooltip>
        </el-form-item>
        <el-form-item label-width="0" v-show="updateButtonVisible">
          <el-button type="primary" @click="updateUser">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped lang="less">
h2 {
  text-align: center;
  .el-text {
    font-size: 2em;
  }
}

.form-box {
  .el-form {
    width: 300px;

    .el-button {
      width: 100%;
    }

    .show-text {
      min-width: 200px;
      &:hover {
        color: var(--el-color-danger);
      }
    }
  }
}
</style>
